package com.young.scrollchildanim;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;

/**
 * Author：young
 * .
 * Time： 2022/5/7 10:24 周六
 * .
 * Desc：滚动动画子视图的容器
 * <p>
 * 在这里会做一些偷梁换柱的操作
 */
public class ScrollAnimChildContainerLayout extends LinearLayoutCompat {

    public ScrollAnimChildContainerLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public ScrollAnimChildContainerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ScrollAnimChildContainerLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 内部会固定垂直方向(所以当前方法废弃)
     *
     * @param orientation 视图的方向
     */
    @Deprecated
    @Override
    public void setOrientation(int orientation) {
        //super.setOrientation(orientation);
    }

    /**
     * 参考：https://blog.csdn.net/wjr1949/article/details/105607740
     * 当前方法会被  LayoutInflater.from(getContext()).inflate 中 的 tryInflatePrecompiled 进行调用
     *
     * @param attrs 属性集合
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new ScrollAnimLayoutParams(getContext(), attrs);
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (params instanceof ScrollAnimLayoutParams) {
            ScrollAnimLayoutParams layoutParams = (ScrollAnimLayoutParams) params;
            if (layoutParams.isNeedMountAnim()) {
                ScrollChildMountAnimView newChild = generateAnimView(layoutParams);
                // 将原本的视图包进需要执行动画的视图
                newChild.addView(child);
                super.addView(newChild, index, params);
            } else {
                // 不需要挂载动画
                super.addView(child, index, params);
            }
        } else {
            super.addView(child, index, params);
        }

    }

    private void init() {
        super.setOrientation(LinearLayoutCompat.VERTICAL);
    }

    /**
     * 生成动画视图
     *
     * @param params 视图的 LayoutParams
     */
    private ScrollChildMountAnimView generateAnimView(ScrollAnimLayoutParams params) {
        ScrollChildMountAnimView view = new ScrollChildMountAnimView(getContext());
        view.setMountAnimFromBgColor(params.mountAnimFromBgColor);
        view.setMountAnimToBgColor(params.mountAnimToBgColor);
        view.setMountAnimScaleX(params.isMountAnimScaleX);
        view.setMountAnimScaleY(params.isMountAnimScaleY);
        view.setMountAnimAlpha(params.isMountAnimAlpha);
        view.setMountAnimTranslation(params.mountAnimTranslation);
        return view;
    }

    public static class ScrollAnimLayoutParams extends LinearLayoutCompat.LayoutParams {
        public int mountAnimFromBgColor;//背景颜色变化开始值
        public int mountAnimToBgColor;//背景颜色变化结束值
        public boolean isMountAnimAlpha;//是否需要透明度动画
        public int mountAnimTranslation;//平移值
        public boolean isMountAnimScaleX;//是否需要x轴方向缩放
        public boolean isMountAnimScaleY;//是否需要y轴方向缩放


        public ScrollAnimLayoutParams(Context context, AttributeSet attrs) {
            super(context, attrs);
            // 从child里面拿到我自定义的属性
            @SuppressLint("CustomViewStyleable")
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScrollChildMountAnimView_LayoutParams);
            mountAnimFromBgColor = typedArray.getInt(R.styleable.ScrollChildMountAnimView_LayoutParams_mount_anim_fromBgColor, -1);
            mountAnimToBgColor = typedArray.getInt(R.styleable.ScrollChildMountAnimView_LayoutParams_mount_anim_toBgColor, -1);
            isMountAnimAlpha = typedArray.getBoolean(R.styleable.ScrollChildMountAnimView_LayoutParams_mount_anim_alpha, false);
            mountAnimTranslation = typedArray.getInt(R.styleable.ScrollChildMountAnimView_LayoutParams_mount_anim_translation, -1);
            isMountAnimScaleX = typedArray.getBoolean(R.styleable.ScrollChildMountAnimView_LayoutParams_mount_anim_scaleX, false);
            isMountAnimScaleY = typedArray.getBoolean(R.styleable.ScrollChildMountAnimView_LayoutParams_mount_anim_scaleY, false);
            typedArray.recycle();
        }


        /**
         * 释放需要挂载动画
         *
         * @return true: 需要 false：不需要
         */
        public boolean isNeedMountAnim() {
            return isMountAnimAlpha ||
                    isMountAnimScaleX ||
                    isMountAnimScaleY ||
                    mountAnimTranslation != -1 ||
                    (mountAnimFromBgColor != -1 && mountAnimToBgColor != -1);
        }

    }

}
